<?php

namespace app\data\controller\rework;

use app\admin\service\FeiShuService;
use app\admin\service\WxWorkService;
use app\data\service\ReworkService;
use think\admin\Controller;
use think\admin\service\AdminService;
use think\facade\Log;

/**
 * 品质答题
 * Class ReworkAnswerQuestion
 * @package app\data\controller\rework
 */
class ReworkAnswerQuestion extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'rework_question';

    /**
     * 品质要求
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function qualityRequirements()
    {
        $userId = AdminService::instance()->getUserId();

        $this->url = config('fei_shu.url') . '/admin.html?show=answer_question&show_id=1#/data/rework.rework_answer_question/answerQuestion.html?show_id=1';

        $this->canAnswerQuestion = $this->app->db->name('rework_record')->where('uuid_city', $userId)->where('is_unlock', 0)->findOrEmpty() ? 1 : 0;

        $this->fetch('quality_requirements');
    }

    /**
     * 品质答题成功
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function answerQuestionSuccess()
    {
        $this->fetch('answer_question_success');
    }

    /**
     * 品质答题失败
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function answerQuestionFail()
    {
        $userId = AdminService::instance()->getUserId();

        $reworkRecord = $this->app->db->name('rework_record')->where('uuid_city', $userId)->order('id')->where('is_unlock', 0)->findOrEmpty();
        if ($reworkRecord) {
            $this->app->db->name('rework_record')->where('uuid_city', $userId)->update(['send_fail_cont' => ($reworkRecord['send_fail_cont'] + 1)]);


            if ($reworkRecord['send_fail_cont'] % 2 === 0) {
                $openId = $this->app->db->name('work_user')->where('id', $userId)->value('open_id');
                if ($openId) {
                    $messageText = "尊敬的师傅，请重新阅读品质要求准则，所有题目都回答正确，方可解锁成功。<a href='" . config('fei_shu.url') . '/admin.html?show=quality_requirements&show_id=1#/data/rework.rework_answer_question/qualityRequirements.html?show_id=1' . "'>解锁></a>";
                    FeiShuService::instance()->sendMessageTextByUserid($openId, $messageText);
                }
            }
        }


        $this->url = config('fei_shu.url') . '/admin.html?show=quality_requirements&show_id=1#/data/rework.rework_answer_question/qualityRequirements.html?show_id=1';

        $this->fetch('answer_question_fail');
    }

    /**
     * 品质答题
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function answerQuestion()
    {
        if ($this->request->isPost()) {

            $userId = AdminService::instance()->getUserId();

            $reworkRecord = $this->app->db->name('rework_record')->where('uuid_city', $userId)->where('is_unlock', 0)->findOrEmpty();
            if ($reworkRecord) {
                $this->app->db->name('rework_record')->where('uuid_city', $userId)->where('is_unlock', 0)->update(['is_unlock' => 1, 'unlock_at' => date('Y-m-d H:i:s')]);

                $openId = $this->app->db->name('work_user')->where('id', $userId)->value('open_id');
                if ($openId) {
                    $messageText = '尊敬的师傅，您已解锁成功，请遵守品质要求，避免再次被锁:打卡后即可开始接单啦。';
                    FeiShuService::instance()->sendMessageTextByUserid($openId, $messageText);
                }
            }

            $this->fetch('answer_question_success');
        } else {
            $this->questions = $this->app->db->name($this->table)->field('title,answer_a,answer_b,answer_c,answer_d')->where('state', 1)->limit(5)->orderRand()->select()->toArray();

            foreach ($this->questions as &$question) {

                $question['answer_a'] = explode('###', $question['answer_a']);
                $question['answer_b'] = explode('###', $question['answer_b']);
                $question['answer_c'] = explode('###', $question['answer_c']);
                $question['answer_d'] = explode('###', $question['answer_d']);

                if (!empty($question['answer_a'][0] ?? '')) {
                    $question['answers'][] = [
                        'text' => $question['answer_a'][0] ?? '',
                        'correct' => intval($question['answer_a'][1] ?? 0),
                    ];
                }
                if (!empty($question['answer_b'][0] ?? '')) {
                    $question['answers'][] = [
                        'text' => $question['answer_b'][0] ?? '',
                        'correct' => intval($question['answer_b'][1] ?? 0),
                    ];
                }
                if (!empty($question['answer_c'][0] ?? '')) {
                    $question['answers'][] = [
                        'text' => $question['answer_c'][0] ?? '',
                        'correct' => intval($question['answer_c'][1] ?? 0),
                    ];
                }
                if (!empty($question['answer_d'][0] ?? '')) {
                    $question['answers'][] = [
                        'text' => $question['answer_d'][0] ?? '',
                        'correct' => intval($question['answer_d'][1] ?? 0),
                    ];
                }

                shuffle($question['answers']);
                unset($question['answer_a'], $question['answer_b'], $question['answer_c'], $question['answer_d']);
            }

            $this->fetch('answer_question');
        }
    }

}